package com.wanmait.schedule.exception;

import com.wanmait.schedule.util.AjaxResult;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MyControllerAdvice {

    /**
     * 教师后台登录没有携带token
     * @param e
     * @return
     */
    @ExceptionHandler(TeacherTokenException.class)
    @ResponseBody
    public AjaxResult handlerException(Exception e){
        //获取异常信息存入ResponseResult的msg属性
        String message = e.getMessage();
        //作为返回值返回，要求转化成json存入响应体中
        return AjaxResult.error(1005,message);
    }

    /**
     * 未授权异常
     * @param e
     * @return
     */
    @ExceptionHandler({AuthorizationException.class})
    @ResponseBody
    public AjaxResult handlerNotAuthorized(Exception e){
        String message = e.getMessage();
        return AjaxResult.error(1006,message);
    }

    /**
     * token过期
     * @param e
     * @return
     */
    @ExceptionHandler(AuthenticationException.class)
    @ResponseBody
    public AjaxResult handleNotAuthorization(Exception e){
        String message = e.getMessage();
        return AjaxResult.error(1005,message);
    }

}
